using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Reflection;
namespace TestAssembly {
	class AsmFileSearch {
		List<string> dirs=new List<string>();
		List<string> dlls=new List<string>();
		static DAL dal=new DAL();
		static void MainX() {
			//dal.NonQry("drop table assembly");
			//dal.NonQry("create table assembly(id int identity primary key,name varchar(150),version varchar(15),path varchar(550)inserted datetime default getdate(),ftime datetime,fsize int)");
			//"create unique index IU_assembly_path on assembly(path)"
			dal.NonQry("truncate table assembly");
			//dal.NonQry("alter table assembly add runTime varchar(10)");
			AsmFileSearch fs=new AsmFileSearch();
			fs.dirs.Add(@"C:\projects\cs\");
			//fs.dirs.Add(@"\\elm-rsy-drdobby\c$\Windows\Assembly\");
			//fs.dirs.Add(@"\\pon-wprf01-cl01\c$\Program Files\");
			//fs.dirs.Add(@"\\pon-wprf01-cl01\c$\Program Files\Next.Whs.DirectoryPicking.NewRF");
			//fs.dirs.Add(@"\\pon-wprf01-cl01\c$\Program Files\RF");
			//fs.dirs.Add(@"\\pon-wprf02-cl01\c$\Program Files\");
			//fs.dirs.Add(@"\\pon-wprf03-cl01\c$\Program Files\");
			//fs.dirs.Add(@"\\pon-wprf05-cl01\c$\Program Files\");
			//fs.dirs.Add(@"\\pon-wprf06-cl01\c$\Next\");
			//fs.dirs.Add(@"\\pon-wprf07-cl01\c$\Next\");
			//fs.dirs.Add(@"\\pon-wprf07-cl02\c$\Next\");
			//fs.dirs.Add(@"\\pon-wprf07-cl01\c$\Windows\Assembly\");
			//fs.dirs.Add(@"\\pon-wprf07-cl02\c$\Windows\Assembly\");
			//fs.dirs.Add(@"\\pon-wpwb01-cl01\c$\PON-WPWB01-DFS\wwwroot");
			//fs.dirs.Add(@"\\pon-wpwb01-cl01\c$\Program Files\NextAssemblies");
			//fs.dirs.Add(@"\\pon-wpwb03-cl01\c$\Program Files\");
			//fs.dirs.Add(@"\\pon-wpas10-cl01\c$\Program Files\");
			//fs.dirs.Add(@"\\pon-wpmq01-cl01\c$\Next.Whs.Assemblies");
			//fs.dirs.Add(@"\\pon-wpct01\cdrive$\Program Files (x86)\Warehouse Systems");
			//fs.dirs.Add(@"\\pon-wpwb03-cl01\c$\Windows\assembly\");
			foreach(string dir in fs.dirs)
				fs.CheckDirs(new DirectoryInfo(dir));
		}
		void CheckDirs(params DirectoryInfo[] arr) {
			foreach(DirectoryInfo dir in arr)
			{
				foreach(FileInfo fi in dir.GetFiles("*.dll"))
				{
					try
					{
						System.Reflection.AssemblyName testAssembly=System.Reflection.AssemblyName.GetAssemblyName(fi.FullName);
						Assembly asm=Assembly.LoadFrom(fi.FullName);
						//Console.WriteLine(asm.ImageRuntimeVersion);
						string hash="";
						//using(MD5 md5=MD5.Create())
						//using(Stream stream=File.OpenRead(fi.FullName))
						//  hash=BitConverter.ToString(md5.ComputeHash(stream)).Replace("-","").ToLower();
						dal.NonQry("insert into assembly(name,version,path,fCreTime,fModTime,fAccTime,fsize,hash,procArch,runTime)values('{0}','{1}','{2}','{3:yyyy-MM-dd HH:mm:ss}','{4:yyyy-MM-dd HH:mm:ss}','{5:yyyy-MM-dd HH:mm:ss}',{6},'{7}','{8}','{9}')",testAssembly.Name,testAssembly.Version,fi.FullName,fi.CreationTime,fi.LastWriteTime,fi.LastAccessTime,fi.Length,hash,testAssembly.ProcessorArchitecture,asm.ImageRuntimeVersion);
					}
					catch(Exception ex)
					{
						//Console.WriteLine(ex);
						string hash="";
						using(MD5 md5=MD5.Create())
						using(Stream stream=File.OpenRead(fi.FullName))
							hash=BitConverter.ToString(md5.ComputeHash(stream)).Replace("-","").ToLower();
						dal.NonQry("insert into assembly(name,version,path,fCreTime,fModTime,fAccTime,fsize,hash)values('{0}','{1}','{2}','{3:yyyy-MM-dd HH:mm:ss}','{4:yyyy-MM-dd HH:mm:ss}','{5:yyyy-MM-dd HH:mm:ss}',{6},'{7}')",fi.Name,"-1",fi.FullName,fi.CreationTime,fi.LastWriteTime,fi.LastAccessTime,fi.Length,hash);
					}
					//dlls.Add(fi.FullName);
				}
				CheckDirs(dir.GetDirectories());
			}
		}
	}
}
